package DBLayer;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import modelLayer.Product;
import modelLayer.Supplier;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DBProductTest {

	private static DBProduct dbProduct = new DBProduct();
	private static DBSupplier dbSupplier = new DBSupplier();

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testGetAllProducts() {
		List<Product> list = new ArrayList<Product>();

		list = dbProduct.getAllProducts(false);

		if(list.isEmpty())
		{
			System.out.println("product list is empty");
		}
		else
		{

			for(Product p: list)
			{
				System.out.println("#---------------------------------------------#");
				System.out.println("productID: "+p.getProductID());
				System.out.println("productID: "+p.getName());
				System.out.println("#---------------------------------------------#");
				System.out.println();
			}

		}


	}

	@Test
	public void testFindProduct() {
		Product prObj = new Product();

		prObj = dbProduct.findProduct("123456781", true);

		System.out.println("find Product by ID");
		System.out.println("#---------------------------------------------#");
		System.out.println("productID: "+prObj.getProductID());
		System.out.println("productID: "+prObj.getName());
		System.out.println("#---------------------------------------------#");
		System.out.println();
		System.out.println();

	}

	@Test
	public void testInsertProduct() {
		Supplier supObj =  dbSupplier.findSupplier("909106085", false);

		Product prObj = new Product();
		prObj.setName("test");
		prObj.setPurchasePrice(200.00);
		prObj.setRentPrice(600.00);
		prObj.setSalesPrice(1000.99);
		prObj.setContryOfOrgin("muhammed-land");
		prObj.setMinStock(50);
		prObj.setSupplier(supObj);
		prObj.setType("test");

		try {
			if(dbProduct.insertProduct(prObj, false) == 1)
			{
				int prObjID = GetMax.getMaxId("select max(productID) FROM Product");
				
				Product pr = new Product();
				
				
				pr = dbProduct.findProduct(String.valueOf(prObjID), true);
				
				System.out.println("Insert of product succesed: ");
				System.out.println("  product id : "+ pr.getProductID());
				System.out.println("product name : "+ pr.getName());
				System.out.println(" supplier id : "+ pr.getSupplier().getName());

			}



		}
		

	catch (Exception e) {
		// TODO Auto-generated catch block
		System.out.println("Insert of product error: "+e);
	}




}

@Test
public void testDeleteProduct() {
	fail("Not yet implemented");
}

}
